package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarSharpDeceleration;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 东风急减速算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 * @author web
 */
public class SharpDeceleration_DF extends AbstractArithmetic<VarSharpDeceleration> {
    /**
     * 事件类型
     */
    private final EventType EVENT_TYPE = EventType.sharpDeceleration_DF;
    /**
     * 急减速加速度阀值，默认-4m/s2
     */
    private Double acceleratedSpeedThreshold = ConfigMap.getDouble("DF_Dec_Threshold");

    /**
     * 判断当前是否为急减速
     */
    boolean isCurSharpDeceleration() {
        return (drivingData!=null && getCommonData().isBrake() && getCommonData().getAcceleration()<acceleratedSpeedThreshold);
    }

    @Override
    public void calculate() {
        logger.debug("进入东风急减速算法.[terminalId:{}]", terminalId);
        VarSharpDeceleration var = super.getObj();

        boolean preSharpDeceleration = var.isPreSharpDeceleration();
        boolean curSharpDeceleration = isCurSharpDeceleration();
        logger.debug("东风急减速算法，急减速状态：preSharpDeceleration:{},curSharpDeceleration:{}.[terminalId:{}]", preSharpDeceleration, curSharpDeceleration, terminalId);

        //算法计算
        switch (getEventStatus(preSharpDeceleration, curSharpDeceleration)) {
            case START:
                logger.debug("东风急减速算法，事件开始.[terminalId:{}]", terminalId);
                startEventData(EVENT_TYPE);
                break;
            case IN:
                logger.debug("东风急减速算法，事件中.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                break;
            case END:
                logger.debug("东风急减速算法，事件结束.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                endEventData();
                break;
            default:
                logger.debug("东风急减速算法，急减速状态：上一次和本次都未急减速，跳出算法.[terminalId:{}]", terminalId);
        }


        //行程结束时要清理对应的未结束事件
        if (getCommonData().isTripLastNode()) {
            endEventData();
        }

        //设置上一个点状态、供下个点计算
        var.setPreSharpDeceleration(curSharpDeceleration);
        logger.debug("东风急减速算法结束.[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarSharpDeceleration var = getObj();
        statisticDataOut.setSharpDownSpeedMileage_DF(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setSharpDownSpeedFuel_DF(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setSharpDownSpeedDuration_DF(ArithmeticUtils.timeMsToSecond(var.getDuration()));
        statisticDataOut.setSharpDownSpeedNumber_DF(var.getNumber());
    }

    private void endEventData() {
        endEventData(EVENT_TYPE, eventData -> {
            VarSharpDeceleration var = getObj();
            var.addNumber(1);
            var.addMileage( Optional.ofNullable(eventData.getExtendField05()).orElse(0.0));
            var.addFuel( Optional.ofNullable(eventData.getExtendField06()).orElse(0.0).intValue());
            var.addDuration( Optional.ofNullable(eventData.getDuration()).orElse(0L).intValue());
        });
    }
}
